###################################################
# Service Deployment Script Using Cmdlets         #
#                                                 #
# November 2011                                   #
# Christian Hollerup Mikkelsen                    #
#                                                 #
# See:                                            #
# http://code.google.com/p/msbuild-devops-tasks/  #
#                                                 #
###################################################

clear

Write-Host ""
Write-Host ""
Write-Host "Service Deployment Script" -foregroundcolor darkgray
Write-Host "----------------------------------------------"  -foregroundcolor darkgray
Write-Host ""
Write-Host "November 2011"  -foregroundcolor darkgray
Write-Host "Christian Hollerup Mikkelsen"  -foregroundcolor darkgray
Write-Host ""
Write-Host "See:"  -foregroundcolor darkgray
Write-Host "http://code.google.com/p/msbuild-devops-tasks/"  -foregroundcolor darkgray
Write-Host ""
Write-Host "----------------------------------------------"  -foregroundcolor darkgray

$taskPath = resolve-path "..\..\..\Output\DotNet.DevOps.Tasks.dll"
$ionicPath = resolve-path "..\..\..\Output\Ionic.Zip.Reduced.dll"

import-module $taskPath
import-module $ionicPath

$validArgs = @("dev","test","prod")


if($args.length -le 0 -Or ($validArgs -contains $args[0]) -eq $false)
{
    Write-Host ""
    Write-Host "Usage: deploy env" -foregroundcolor yellow
    Write-Host "  Where env is one of: dev, test, prod" -foregroundcolor yellow
    Write-Host ""
    exit
}

if ($args[0] -eq "prod" -And $env:username -ne "Master Garrett")
{
    Write-Host ""
    Write-Host "You're not APP_USER and can therefore not run this script with PRODUCTION settings!`nPlease re-run this script as APP_USER on the correct SERVER!" -foregroundcolor red
    Write-Host ""
    exit
}

#######################
#     SETUP PROPS     #
#######################
# Global props
$globalProps = @{
    AppConfig = resolve-path ".\bin\Debug\Deploy\TestService.exe.config";
    Log4net = resolve-path ".\bin\Debug\Deploy\log4net.config";
}

# Env props
if($args[0] -eq "dev" )
{
    Write-Host "Using DEVELOPMENT settings:" -foregroundcolor green

    $serviceProps = @{
        Name              = "TestService";
        Machine           = "";
        LogonAsUser       = "";
        LogonAsPassword   = "";
        ServiceExecutable = "TestService.exe";
        ServiceArguments  = "";
        SourcePath        = resolve-path ".\bin\Debug";
        DestinationPath   = resolve-path ".\bin\Debug\Deploy";
        Description       = "My scripted TestService";
        StartWhenDone     = $false;
        Startup           = "auto";
        Timeout           = 15000;
    }

    $globalProps["DataSource"] = "DEV"
    $globalProps["RootLogger"] = "WARN"
    $globalProps["NHBLogger"] = "WARN"
}

if($args[0] -eq "test" )
{
    Write-Host "Using TEST settings:" -foregroundcolor yellow

    $serviceProps = @{
        Name              = "TestService";
        Machine           = "";
        LogonAsUser       = "";
        LogonAsPassword   = "";
        ServiceExecutable = "TestService.exe";
        ServiceArguments  = "";
        SourcePath        = resolve-path ".\bin\Debug";
        DestinationPath   = resolve-path ".\bin\Debug\Deploy";
        Description       = "My scripted TestService";
        StartWhenDone     = $false;
        Startup           = "auto";
        Timeout           = 15000;
    }

    $globalProps["DataSource"] = "DEV"
    $globalProps["RootLogger"] = "WARN"
    $globalProps["NHBLogger"] = "WARN"
}

if($args[0] -eq "prod" )
{
    $user=read-host "Enter user which this service will run as (ex. BI\APP_USER):"
    $pass=read-host "Password: "
    
    Write-Host "Using PRODUCTION settings:" -foregroundcolor red

    $serviceProps = @{
        Name              = "TestService";
        Machine           = "";
        LogonAsUser       = $user;
        LogonAsPassword   = $pass;
        ServiceExecutable = "TestService.exe";
        ServiceArguments  = "";
        SourcePath        = resolve-path ".\bin\Debug";
        DestinationPath   = resolve-path ".\bin\Debug\Deploy";
        Description       = "My scripted TestService";
        StartWhenDone     = $false;
        Startup           = "auto";
        Timeout           = 15000;
    }

    $globalProps["DataSource"] = "DEV"
    $globalProps["RootLogger"] = "WARN"
    $globalProps["NHBLogger"] = "WARN"
}

Write-Output $serviceProps 
Write-Host ""
Write-Output $globalProps 
Write-Host "----------------------------------------------"


#Install-Service $serviceProps

Install-Service     -Name               $serviceProps["Name"] `
                    -Machine            $serviceProps["Machine"] `
                    -LogonAsUser        $serviceProps["LogonAsUser"] `
                    -LogonAsPassword    $serviceProps["LogonAsPassword"] `
                    -ServiceExecutable  $serviceProps["ServiceExecutable"] `
                    -ServiceArguments   $serviceProps["ServiceArguments"] `
                    -SourcePath         $serviceProps["SourcePath"] `
                    -DestinationPath    $serviceProps["DestinationPath"] `
                    -Description        $serviceProps["Description"] `
                    -StartWhenDone      $serviceProps["StartWhenDone"] `
                    -Startup            $serviceProps["Startup"] `
                    -Timeout            $serviceProps["Timeout"] 

 
Update-AppSetting -Files $globalProps["AppConfig"] `
                  -Key   "DataSource" `
                  -Value $globalProps["DataSource"] 
                         

Update-RootLevel -Files $globalProps["Log4net"] `
                 -Level $globalProps["RootLogger"]
                                                      
Update-LoggerLevel -Files $globalProps["Log4net"] `
                   -Name  "NHibernate" `
                   -Level $globalProps["NHBLogger"]
                                                           
Start-Service -Name $serviceProps["Name"] `
              -Machine $serviceProps["Machine"]
                                                     
